////////////////////////////////////////////////////////////////////////////
//
//  CryEngine Source File.
//  Copyright (C), Crytek Studios, 2001-2006.
// -------------------------------------------------------------------------
//  File name:   XmlUtils.h
//  Created:     21/04/2006 by Timur.
//  Description: 
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////

#ifndef __XmlUtils_h__
#define __XmlUtils_h__
#pragma once

#include "ISystem.h"

#ifdef _RELEASE
#define CHECK_STATS_THREAD_OWNERSHIP()
#else
#define CHECK_STATS_THREAD_OWNERSHIP() if (m_statsThreadOwner!=CryGetCurrentThreadId()) __debugbreak();
#endif

class CXmlNodePool;

//////////////////////////////////////////////////////////////////////////
// Implements IXmlUtils interface.
//////////////////////////////////////////////////////////////////////////
class CXmlUtils : public IXmlUtils, public ISystemEventListener
{
public:
	CXmlUtils( ISystem *pSystem );
	virtual ~CXmlUtils();

	//////////////////////////////////////////////////////////////////////////
	// IXmlUtils
	//////////////////////////////////////////////////////////////////////////
#if !defined(RESOURCE_COMPILER)
	VIRTUAL IXmlParser* CreateXmlParser();
#endif
	// Load xml from file, returns 0 if load failed.
	VIRTUAL XmlNodeRef LoadXmlFromFile( const char *sFilename, bool bReuseStrings = false );
	// Load xml from memory buffer, returns 0 if load failed.
	VIRTUAL XmlNodeRef LoadXmlFromBuffer( const char *buffer, size_t size, bool bReuseStrings = false );	

	// create an MD5 hash of an XML file
	VIRTUAL const char * HashXml( XmlNodeRef node );

	// Get an object that can read a xml into a IReadXMLSink 
	// and write a xml from a IWriteXMLSource
	VIRTUAL IReadWriteXMLSink* GetIReadWriteXMLSink();

	VIRTUAL IXmlSerializer* CreateXmlSerializer();

#if !defined(RESOURCE_COMPILER)
	VIRTUAL bool SaveBinaryXmlFile( const char *sFilename,XmlNodeRef root );
	VIRTUAL XmlNodeRef LoadBinaryXmlFile( const char *sFilename );

	VIRTUAL bool EnableBinaryXmlLoading( bool bEnable );

	// Create XML Table reader.
	VIRTUAL IXmlTableReader* CreateXmlTableReader();
	//////////////////////////////////////////////////////////////////////////

	//////////////////////////////////////////////////////////////////////////
	// ISystemEventListener
	//////////////////////////////////////////////////////////////////////////
	virtual void OnSystemEvent( ESystemEvent event,UINT_PTR wparam,UINT_PTR lparam );

	//////////////////////////////////////////////////////////////////////////
	// Init xml stats nodes pool
	//////////////////////////////////////////////////////////////////////////
	VIRTUAL void InitStatsXmlNodePool( uint32 nPoolSize = 1024*1024 );

	// Create new xml node for statistics
	VIRTUAL XmlNodeRef CreateStatsXmlNode( const char *sNodeName="" );
	
	// Set owner thread
	VIRTUAL void SetStatsOwnerThread( DWORD threadId );

	// Free memory if stats xml node pool is empty
	VIRTUAL void FlushStatsXmlNodePool();
#endif

	//////////////////////////////////////////////////////////////////////////
	virtual void GetMemoryUsage( ICrySizer *pSizer );
private:
	ISystem *m_pSystem;
	IReadWriteXMLSink*    m_pReadWriteXMLSink;
	CXmlNodePool*					m_pStatsXmlNodePool;
#ifndef _RELEASE
	DWORD m_statsThreadOwner;
#endif
};

#endif //__XmlUtils_h__
